<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>采购需求 - 产品数据管理系统</title>
{include file="demand_header.php"}
<style>
{literal}
.box-body{min-height:350px;}
.table-toolbar .columns-right{position:relative;}
.table-toolbar .dropdown-menu{right:0;left:auto;max-height:340px;overflow:auto;}
.table-toolbar .dropdown-menu li{padding:5px 15px;}
.table-toolbar .dropdown-menu li p{margin:0;}
.img-thumbnail{width:50px;height:50px;}
.nav-tabs-custom{box-shadow:none;margin:10px 10px 0px 10px;}
.slct{display:inline-block; width:auto;}
{/literal}
</style>
</head>
<body class="skin-blue">
<div class="wrapper">
	{include file="bread_crumbs.php"}
	<!--引入面包屑-->
	<section class="base-setting-wrap">
		<div class="row">
			<div class="col-md-12">
				<div class="box">
					<div class="nav-tabs-custom">
						<ul class="nav nav-tabs">
							<li><a href="/backup/demand">采购需求列表</a></li>
							<li class="active"><a href="/backup/demand/reject-list">需求待确认列表</a></li>
							<li><a href="/backup/demand/story-intercept">需求拦截列表</a></li>
							<li><a href="/backup/log/list">采购需求产生日志</a></li>
						</ul>
					</div>
	                <div class="box-header with-border clearfix">
	                	<div class="table-toolbar pull-right mb10 clearfix">
						  <div class="columns-right btn-group pull-right">
						    <button class="btn btn-default btn-sm" type="button" title="刷新" onClick="window.location.reload();"><i class="glyphicon glyphicon-refresh icon-refresh"></i></button>
						    <div class="keep-open btn-group" title="列">
						      <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="glyphicon glyphicon-th icon-th"></i> <span class="caret"></span></button>
						        <ul class="dropdown-menu" id="js_productOption"> 
								</ul>
						    </div>
						  </div>
						</div>
	                	<div class="box-form pull-right form-group-sm mr20">
	                		<p class="pull-right"><a href="javascript:;" class="btn btn-success btn-sm" id="js_toggleSearchBox"><i class="fa fa-search"></i><span class="pl5">搜索选项</span></a></p>
	                	</div>
	                	<div class="clearfix"></div>
						<div class="search-box" id="js_searchBox">
							<form method="get" action="/backup/demand/reject-list" class="form-inline auto-restore" id="search_form">
								<div class="form-group-sm">
									<p>
										<input type="hidden" name="from_warehouse" id="from_wh"/>
										<input type="hidden" name="to_warehouse" id="to_wh"/>	
										产品分类:<select id="cat_id" name="product_category" data-path='{$queryParameter["catalogPath"]}' style="font-size:12px;font-family:;microsoft YaHei;">
										</select>
										<input type="hidden" name="product_category"  id="select_cat_id"/>&nbsp;
									    <label class="control-label ml10">搜索关键字类别：</label>
										<select class="form-control" name="search_keyword_type" id="search_type">
			                				<option value="">请选择</option>
			                				<option value="goods_sn">产品编码</option>
			                				<option value="product_name">产品名称</option>
		                				</select>
										<input type="text" class="form-control" name="search_keyword">
                                        <br/>
                                        <br/>
										<label class="control-label">所属事业部：</label>
										<select class="form-control" id="dept_id" name="dept_id">
							    			<option value="">选择事业部</option>
                                                 {foreach from=$hasWarehouseDep item=oneDep}
                                           <option value="{$oneDep['id']}">{$oneDep['department_name']}</option>
                                         {/foreach}
										</select>
					    			    <select name="warehouse" class="form-control" id="dep_warehouse" name="warehouse" style+"width:200px;">
					    				   <option value="">选择仓库</option>
					    			    </select>
										<button type="button" class="btn btn-primary btn-sm" id="search_button">立即搜索</button>
									</p>
								</div>
							</form>
						</div>
	                </div>
	                <!-- /.box-header -->
					<form action="/demand/index/examine" method="post">
		                <div class="box-body">
		                  <table class="table table-bordered table-striped table-hover" id="js_productTable">
		                  	<thead>
		                    <tr>
		                      <th><input type="checkbox" name="id[]" value="" /></th>
		                      <th data-field="goods_sn" data-checked="{if in_array('goods_sn',$productTableFiled)}true{else}false{/if}">产品编码</th>                      
		                      <th data-field="thumb_url" data-checked="{if in_array('thumb_url',$productTableFiled)}true{else}false{/if}">产品图片</th>
		                      <th data-field="product_name" data-checked="{if in_array('product_name',$productTableFiled)}true{else}false{/if}">产品名称</th>
		                      <th data-field="purchase_price" data-checked="{if in_array('purchase_price',$productTableFiled)}true{else}false{/if}">采购价</th>
		                      <th data-field="actualsales" data-checked="{if in_array('actualsales',$productTableFiled)}true{else}false{/if}">备货销量</th>
		                      <th data-field="backupsales" data-checked="{if in_array('backupsales',$productTableFiled)}true{else}false{/if}">实际销量</th>
		                      <th data-field="a_story_quantity" data-checked="{if in_array('a_story_quantity',$productTableFiled)}true{else}false{/if}">A需求数量</th>
		                      <th data-field="b_story_quantity" data-checked="{if in_array('b_story_quantity',$productTableFiled)}true{else}false{/if}">B需求数量</th>
		                      <th data-field="c_story_quantity" data-checked="{if in_array('c_story_quantity',$productTableFiled)}true{else}false{/if}">C需求数量</th>
		                      <th data-field="abc_story_amount" data-checked="{if in_array('abc_story_amount',$productTableFiled)}true{else}false{/if}">需求总数</th>
		                      <th data-field="purchaser" data-checked="{if in_array('purchaser',$productTableFiled)}true{else}false{/if}">产品信息</th>
		                      <th data-field="numbers" data-checked="{if in_array('numbers',$productTableFiled)}true{else}false{/if}">数量信息</th>
		                      <th data-field="platform_warehouse" data-checked="{if in_array('platform_warehouse',$productTableFiled)}true{else}false{/if}">平台仓</th>
		                      <th data-field="warehouse_code" data-checked="{if in_array('warehouse_code',$productTableFiled)}true{else}false{/if}">存放仓库</th>
		                      <th data-field="reject_handler" data-checked="{if in_array('reject_handler',$productTableFiled)}true{else}false{/if}">驳回人</th>
		                      <th data-field="reject_datetime" data-checked="{if in_array('reject_datetime',$productTableFiled)}true{else}false{/if}">驳回时间</th>
		                      <th data-field="reject_reason_type" data-checked="{if in_array('reject_reason_type',$productTableFiled)}true{else}false{/if}">驳回原因</th>
		                      <th data-field="reject_reason_detail" data-checked="{if in_array('reject_reason_detail',$productTableFiled)}true{else}false{/if}">驳回详细描述</th>
		                      <th data-field="handler" data-checked="{if in_array('handler',$productTableFiled)}true{else}false{/if}">处理人</th>
		                      <th data-field="reject_result" data-checked="{if in_array('reject_result',$productTableFiled)}true{else}false{/if}">处理结果</th>
		                      <th data-field="operate" data-checked="{if in_array('operate',$productTableFiled)}true{else}false{/if}">操作</th>
		                    </tr>
		                    </thead>
		                    {foreach from=$rejectStoryList item="story"}
		                    <tr>
								<td><input type="checkbox" name="checkitem"></td>	                    	
								<td>{$story.goods_sn}</td>
								<td>{if !empty($story['thumb_url'])} <img src="{$pdmDomain}{$story.thumb_url}">{/if}</td>
								<td>{$story.product_name}</td>
								<td>{$story['purchase_price']}</td>
								<td>
									前7天：{$story['the7DayActualSales']}<br/>
									前15天：{$story['the15DayActualSales']}<br/>
									前30天：{$story['the30DayActualSales']}
								</td>
								<td>
					                前7天：{$story['the7DayBackupSales']}<br/>
									前15天：{$story['the15DayBackupSales']}<br/>
									前30天：{$story['the30DayBackupSales']}
								</td>
								<td class="abc_story_quantity" {if $story.a_story_quantity_modify_log!=""} style="color:red" {/if} data-modify-log="{$story.a_story_quantity_modify_log}" >{$story.a_story_quantity}</td>
								<td class="modify_quantity abc_story_quantity" data-story-id="{$story['story_id']}" data-story-type='b' {if $story.b_story_quantity_modify_log!=""} style="color:red"{/if} data-modify-log="{$story.b_story_quantity_modify_log}" >{$story.b_story_quantity}
								</td>
								<td class="modify_quantity abc_story_quantity" data-story-id="{$story['story_id']}" data-story-type='c' {if $story.c_story_quantity_modify_log!=""} style="color:red" {/if} data-modify-log="{$story.c_story_quantity_modify_log}">   {$story.c_story_quantity}
								</td>
								<td class="abc_story_amount">{math equation='x + y + z' x=$story.a_story_quantity y=$story.b_story_quantity z=$story.c_story_quantity}</td>
								<td>
                                    采购员:{$story['purchaser']}<br/>
									产品状态：{$productStatus[$story.product_status]|default:""}<br />
									缺货周期:{$story['out_cycle']}
								</td>
								<td>
									可用库存：{$story['base_stock']}<br />
									订货数量：{$story['stock_order_nums']}<br />
									最小起订量：{$story['min_order']}
								</td>
								<td>{$story.platform_warehouse}</td>
								<td>{$story.warehouse_code}</td>
								<td>{$story.reject_handler}</td>
								<td>{$story.reject_datetime}</td>
								<td>{$story.reject_reason_type}</td>
								<td>{$story.reject_reason_detail}</td>
								<td>{$story.handler}</td>
								<td class="reject_result">{$story.reject_result}</td>
								<td>
								   {if $story.reject_result=="待处理" }
								    <input type="button" class="handle_story" data-story-id="{$story['story_id']}" data-operate-type="cancle"    value="取消"><br/>
								   <input type="button" class="handle_story" data-story-id="{$story['story_id']}" data-operate-type="purchase" value="采购"><br/>
								   {/if}
								</td>
							</tr>
							{/foreach}
		              	</table>
		                </div>
		                <!-- .box-body -->
	                </form>
	                <div class="box-footer text-center clearfix">
					{$pageLink}
	                </div>
	              </div>
			</div>
		</div>
	</section>
</div>
{include file='foot.tpl'}
<link rel="stylesheet" type="text/css" href="{$js.url}plugins/select2/css/select2.min.css">	
<script type="text/javascript" src="{$js.url}plugins/select2/js/select2.min.js"></script>
<script type="text/javascript">
{literal}

//仓库选择
$('#dept_id').change(dept);
function dept(did){
	var did = !isNaN(did) ? did : $(this).find('option:selected').val();
	var str = '<option value="0">选择仓库</option>';
	var select_warehouse=GLOBAL.getUrlParam('warehouse');
	$.ajax({
		url : '/base/warehouse/getselectedwarehousebydeptid',
		data : {did : did},
		dataType : 'json',
		type : 'get',
		success : function (result){
			$.each(result, function(i, n){
				if(select_warehouse && select_warehouse==n.warehouse_code){
                		str += '<option value="' + n.warehouse_code + '" selected>' + n.warehouse_name + '</option>' + "\r\n";
                	}else{
                		str += '<option value="' + n.warehouse_code + '">' + n.warehouse_name + '</option>' + "\r\n";
                }
			});
			$('#dep_warehouse').html(str);
			$('#dep_warehouse').select2();
		}
	});
}
{/literal}

{literal}
dept(GLOBAL.getUrlParam('dept_id'));

//编辑备货数量
$(function (){
	$('.modify_quantity').dblclick(function (){
		clicked = $(this);
		var story_reject_result=clicked.parent().find('.reject_result').text();
        if(story_reject_result!='待处理'){
        	layer.alert("需求当前状态不允许修改数量！");
        	return false
        }

		var value = clicked.text();
		if(value==0){
			layer.alert("当前需求数量为零，不允许修改数量！");
        	return false
		}
		var dblclick = $(this);
		inputInit();
		$(this).html('<input type="text" style="width:40px;" class="input" value="' + value + '" />');
		$('.input').blur(function (){
			var changeResult=changeInputForText($(this));
			if(changeResult==false){
			   dblclick.remove('input');
			   dblclick.text(value);
			}
		});
		$('input.input').click(function (){
			return false;
		});
		$('input.input').dblclick(function (){
			return false;
		});
		$('.input').keypress(function (event){
			if(event.keyCode == 13){
				var changeResult=changeInputForText($(this));
				if(changeResult==false){
			       dblclick.remove('input');
			       dblclick.text(value);
			   }
				return false;
			}
		});
		function inputInit(){
			$('.modify_quantity').each(function (){
				$(this).html($(this).find('input').val());
			});
			$('.modify_quantity input').remove();
		}
		function changeInputForText(obj){
			if(isNaN(obj.val())){
				layer.msg('错误：请只输入阿拉伯数字！');
				return false;
			}
			if(obj.val()<0){
	            layer.alert('不能小于0！');
				return false;
			}

		    if(obj.val()>value*(1+0.3)){
	            layer.alert('不能增加超过原值的30%！');
				return false;
			}

			var editedNum = clicked.parent().find('.abc_story_quantity');
			var abcAmount = 0;
			dblclick.remove('input');
			dblclick.text(obj.val());
			$.each(editedNum, function (){
				abcAmount += parseInt($(this).text());
			});
			clicked.parent().find('.abc_story_amount').text(abcAmount);

			layer.confirm("确定要修改需求的数量？", function(){
                    sendEdit();
			},function(){
				   dblclick.remove('input');
			       dblclick.text(value);
			});
			return true
		}

		// 修改确认后的操作
		function sendEdit(){
			var story_id=clicked.data('story-id');
			var story_type=clicked.data('story-type');
            var quantity=clicked.text()
		   	$.ajax({
				   type: "get",
				   url: '/backup/demand/modify-quantity',
				   data: {story_id:story_id,story_type:story_type,quantity:quantity},
				   success: function(result){
				     layer.msg( result.msg );
		              if(result.success){
				       location.reload();
		              }
				   }
		    });
	
		}
	});
})

//全选反选
$.fn.check({checkall_name: "id[]", checkbox_name:"checkitem"});

//产品分类
var catalogPath=$("#cat_id").attr("data-path");
var opts = {
	ajax: '/widget/category/getcat',
	selClass:'form-control inline',
	selStyle: 'font-size:12px;font-family:;microsoft YaHei;',
	autoLink : false,
	defVal:catalogPath.split(','),
	head: '请选择产品分类',
	select:  '#cat_id',
};
var linkageSel = new LinkageSel(opts);
$('#search_button').click(function(){
	var v = linkageSel.getSelectedValue();
	$('#select_cat_id').val(v);
	$("#search_form").submit();
});

//需求处理
$(".handle_story").bind("click",function(){
    var operate_type=$(this).data('operate-type');
    	var story_id=$(this).data('story-id');
    var url;
    var operate_name;
    if(operate_type=="cancle"){
    	url='/backup/demand/cancle'
    	operate_name="取消需求";
    }else if(operate_type=="purcahse"){
    	url='/backup/demand/purcahse'
    	operate_name="采购需求";
    }


	layer.confirm("确定要"+operate_name+"？", function(){
        $.ajax({
		   type: "get",
		   url: url,
		   data: {story_id:story_id},
		   success: function(result){
		     layer.msg( result.msg );
              if(result.success){
		       location.reload();
              }
		   }
		});
	});
});
$.createTableOption({
	tableBox:"#js_productTable",
	optionBox:"#js_productOption",
	onOptionChange:function(index,obj,field){
		//保存cookie
		if(obj.prop("checked")){
			productTableFiled.push(field);
		}
		else{
			productTableFiled.remove(field);
		}
		setFiledCookie();
	}
});
$("td.abc_story_quantity").hover(
  function () {
	var log = $(this).data('modify-log');
	if(log != ''){
		layer.tips(log, this, {
			skin: 'layui-layer-molv',
			tips: [4, '#3595CC'],
			time:0
		});
	}
  },
  function () {
   layer.closeAll('tips')
  }
);

var productTableFiled = new Array();
//获取勾选显示的字段

$('[data-checked="true"]').each(function(i){
	field = $(this).attr('data-field');
	productTableFiled.push(field);
});
// 存储 cookie
function setFiledCookie(){
	$.cookie('table_field_reject_story',productTableFiled.join(","),{expires: 7, path:'/'});
}
setFiledCookie();

{/literal}
</script>
<script src="{$js.url}form_helper.js"></script>
</body>
</html>
